package com.sunyard.auth.handler;

import com.sunyard.auth.constants.ErrorCode;
import com.sunyard.auth.dal.query.SmAuthUserQuery;
import com.sunyard.auth.exception.BusinessException;
import com.sunyard.auth.service.SmAuthUserService;
import com.sunyard.auth.service.bo.SmAuthUserBO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: member
 * @description:
 * @author: raojinyx@163.com
 * @create: 2020-05-22 01:18
 **/
@Service
@Slf4j
public class SmAuthUserHandler {
    @Autowired
    private SmAuthUserService smAuthUserService;

    /**
     * 查询-不分页
     * @param washCarMemberBO
     * @return
     */
    public List<SmAuthUserBO> getMemberList(SmAuthUserQuery washCarMemberBO) {
        if (ObjectUtils.isEmpty(washCarMemberBO)) {
            return new ArrayList<>();
        }
        return smAuthUserService.findList(washCarMemberBO);
    }

    /**
     * 查询-登录号
     * @param userName
     * @return
     */
    public SmAuthUserBO findByName(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return null;
        }
        SmAuthUserQuery smAuthUserQuery = new SmAuthUserQuery();
        smAuthUserQuery.setUserName(userName);
        List<SmAuthUserBO> list = this.getMemberList(smAuthUserQuery);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        if (list.size() != 1) {
            throw new BusinessException(ErrorCode.MEMBER_IS_ERROR);
        }
        return list.get(0);
    }

}
